<template>
  <div class="city">

    <div class="search_wrap">
      <div class="search">
        <i class="fa fa-search"></i>
        <input v-model="city_val" type="text" placeholder="输入城市名">
      </div>
      <button @click="$router.push({ name:'address',params:{city:city} })">取消</button>
      <!-- <button @click="$router.push({name:'address',params:{city:city}})">取消</button> -->
    </div>

    <div class="location_wrap" style="height:100%" v-if="searchList.length == 0">
      <div class="location">

        <Location @click="selectCity({name:city})" :address="city"/>

      </div>

        <Alphabet @selectCity="selectCity" ref="allcity" :cityInfo="cityInfo" :keys="keys"/>
        
    </div>

    <div class="search_list" v-else>
      <ul>
        <li @click="selectCity(item)" v-for="(item,index) in searchList" :key="index">{{item.name}}</li>
      </ul>
    </div>

  </div>
</template>

<script>
import Location from "../components/Location";
import Alphabet from "../components/Alphabet";
export default {
  name: "City",
  data(){
    return{
      city_val:"",
      cityInfo: null,
      keys: [],
      allCities: [],  //城市信息容器
      searchList: [], //搜索列表容器
    }
  },
   computed: {
    //这里通过vuex获取了city的值，当前city
    city() {
      return (
        this.$store.getters.location.city 
      );
    }
  },
  //页面创建时，加载城市信息
  created(){
    this.getCityInfo();
  },
  watch:{
    city_val(){
      // console.log(this.city_val)
      this.searchCity()
    }
  },
  methods:{
    searchCity(){
      if(!this.city_val){
        this.searchList=[];
      }else{
        // console.log('正在检索城市')
        this.searchList=this.allCities.filter(all=>{
          return all.name.indexOf(this.city_val)!=-1;
        });
        // console.log(this.searchList)
      }
    },
    selectCity(city){
        this.$router.push({name:'address',params:{city:city.name}});
    },
    getCityInfo(){
      this.$axios('/api/posts/cities')
      .then(res=>{
        // console.log(res.data)
        this.cityInfo=res.data;
        //处理key 计算key
        //从data对象里提取出key值,返回可枚举属性组成的数组
        this.keys=Object.keys(res.data);
        // console.log(this.keys);
        // 移除hotcities的key
        this.keys.pop();
        //keys排序
        this.keys.sort();
        // console.log(this.keys);

        //调用子组件Alphabet里面的滚动实例化方法initScroll
        this.$nextTick(()=>{
          this.$refs.allcity.initScroll();
        })
        //获取城市信息
        this.keys.forEach(key=>{
          // console.log(key)
          this.cityInfo[key].forEach(city=>{
            this.allCities.push(city);
          })
        })
        // console.log(this.allCities)

      })
      .catch(err=>{
        console.log(err)
      });
    }
  },
 
  components: {
    Location,
    Alphabet
  }
};
</script>

<style scoped>
.city {
  width: 100%;
  height: 100%;
  overflow: auto;
  box-sizing: border-box;
  padding-top: 45px;
}
.search_wrap {
  position: fixed;
  top: 0;
  height: 45px;
  width: 100%;
  background: #fff;
  box-sizing: border-box;
  padding: 3px 16px;
  display: flex;
  justify-content: space-between;
}
.search {
  background-color: #eee;
  border-radius: 10px;
  line-height: 40px;
  width: 85%;
  box-sizing: border-box;
  padding: 0 16px;
}
.search input {
  background: #eee;
  outline: none;
  border: none;
  margin-left: 5px;
}
.search_wrap button {
  outline: none;
  color: #009eef;
}

.location {
  background: #fff;
  padding: 8px 16px;
  height: 65px;
  box-sizing: border-box;
}

.search_list {
  background: #fff;
  padding: 5px 16px;
}
.search_list li {
  padding: 10px;
  border-bottom: 1px solid #eee;
}
</style>